/*************************************************************************
> File Name   : main.go
> Author      : seadog
> Mail        : seadog0514@163.com
> Version     : 0.1
> Created Time: Fri 2023-10-27 00:32:22(+0000)
************************************************************************/

/* http/https proxy server
 * https://kingsamchen.github.io/2018/06/10/build-your-own-http-proxy-using-go/
 *
 * usage:
 *  start server
 *  test:
 *   export http_proxy="http://127.0.0.1:10088"
 *   export https_proxy="http://127.0.0.1:10088"
 *   curl -v 192.168.0.211:8087
 *   curl -ik https://seadog.pythonabc.org:60555/noteboard/
 *
 */

package main

import (
	log "log/slog"
	"os"

	cli "github.com/urfave/cli/v2"
)

func main() {
	app := cli.NewApp()
	app.Name = "http/https-proxy-server"
	app.Version = "v0.0.1"
	app.Usage = "http/https proxy server"
	app.HideHelpCommand = false

	app.Flags = []cli.Flag{
		&cli.StringFlag{
			Name:    "listen",
			Aliases: []string{"l"},
			Usage:   "listen tcp port",
			Value:   "10088",
		},
	}

	app.Action = func(c *cli.Context) error {
		l := c.String("listen")

		// if c.NumFlags() == 0 || c.NArg() != 0 {
		//   cli.ShowAppHelp(c)
		//   os.Exit(0)
		// }

		server := newServer("0.0.0.0:" + l)
		server.start()

		return nil
	}

	if err := app.Run(os.Args); err != nil {
		log.Error(err.Error())
		os.Exit(1)
	}
}
